<template>
    <div id="dashboard">
        <el-row :gutter="20">
            <el-col :span="8">
                <el-card shadow="hover" class="mgb20" style="height:252px; text-align:center">
                    <img :src="userImgPath" class="user-avator" alt />
                    <div class="user-info">
                        <div class="user-info-cont">
                            <div class="user-info-name">{{ name }}</div>
                            <div>{{ role }}</div>
                            <br>
                            <div>ta很懒 什么都没留下</div>
                        </div>
                    </div>
                    <el-divider></el-divider>
                    <div class="user-info-list" style="text-align: left;">
                        上次登录时间：
                        <span>{{ lastLoginTime }}</span>
                    </div>
                    <div class="user-info-list" style="text-align: left;">
                        上次登录地点：
                        <span>{{ lastLoginArea}}</span>
                    </div>
                </el-card>
                <el-card shadow="hover" style="margin-top:150px;">
                    <template #header>
                        <div class="clearfix">
                            <span>标签</span>
                        </div>
                    </template>
                    <el-tag
                        :key="tag"
                        v-for="tag in dynamicTags"
                        :disable-transitions="false"
                        @close="handleClose(tag)">
                        {{tag}}
                    </el-tag>
                    <!-- <el-input
                        class="input-new-tag"
                        v-if="inputVisible"
                        v-model="inputValue"
                        ref="saveTagInput"
                        size="small"
                        @keydown.enter="handleInputConfirm"
                        @blur="handleInputConfirm">
                    </el-input>
                    <el-button v-else class="button-new-tag" size="small" @click="showInput()">+</el-button> -->
                </el-card>
                <el-card shadow="hover" style="height:252px; margin-top:20px;">
                    <template #header>
                        <div class="clearfix">
                            <span>作品分类</span>
                        </div>
                    </template>
                    Vue
                    <el-progress :percentage="71.3" color="#42b983"></el-progress>JavaScript
                    <el-progress :percentage="24.1" color="#f1e05a"></el-progress>CSS
                    <el-progress :percentage="13.7"></el-progress>HTML
                    <el-progress :percentage="5.9" color="#f56c6c"></el-progress>
                </el-card>
            </el-col>
            <el-col :span="16">
                <el-row :gutter="20" class="mgb20">
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{ padding: '0px' }">
                            <div class="grid-content grid-con-1">
                                <i class="el-icon-user-solid grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{ view_count }}</div>
                                    <div>总访问量</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{ padding: '0px' }">
                            <div class="grid-content grid-con-2">
                                <i class="el-icon-star-on grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{ agree_count }}</div>
                                    <div>点赞数</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{ padding: '0px' }">
                            <div class="grid-content grid-con-3">
                                <i class="el-icon-document grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{ blog_count }}</div>
                                    <div>博文数量</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                </el-row>
                <el-card shadow="hover" style="height:403px;">
                    <template #header>
                        <div class="clearfix">
                            <!-- <span>最热作品</span> -->
                            <el-tabs v-model="activeName" @tab-click="handleClick" type="card">
                                <el-tab-pane label="最热文章" name="hot_article"></el-tab-pane>
                                <el-tab-pane label="最新文章" name="new_article"></el-tab-pane>
                                <el-tab-pane label="最多阅读" name="view_article"></el-tab-pane>
                            </el-tabs>
                            <!-- <el-button style="float: right; padding: 3px 0" type="text" @click="addTask()">新建富文本</el-button> -->
                            <!-- <el-button style="float: right; padding: 3px 0 margin-right: 3px;" type="text" @click="addTask()">新建md</el-button> -->
                        </div>
                    </template>

                    <el-table :show-header="false" :data="todoList" style="width:100%;">
                        <el-table-column width="100">
                            <template #default="scope">
                                <el-tag type="success" v-if="scope.row.is_public==1">{{ scope.row.is_public !== 1 ? "私有" : "公开" }}&nbsp;<i class="el-icon-lx-attention"></i></el-tag>
                                <el-tag type="danger" v-if="scope.row.is_public==0">{{ scope.row.is_public !== 1 ? "私有" : "公开" }}&nbsp;<i class="el-icon-lx-attentionforbid"></i></el-tag>
                            </template>
                        </el-table-column>
                        <el-table-column>
                            <template #default="scope">
                                <div
                                    @click="watch_detail(scope)"
                                    class="todo-item"
                                    :class="{
                                        'todo-item-del': !scope.row.is_public,
                                    }"
                                >{{ scope.row.title }}</div>
                            </template>
                        </el-table-column>
                        <el-table-column width="220">
                            <template #default="scope">
                                    <el-rate
                                        v-model="scope.row.rate_star"
                                        :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
                                        disabled
                                        :texts="text"
                                        allow-half
                                        void-icon-class="el-icon-lx-hotfill"
                                        :show-text=true
                                        text-color="red">
                                    </el-rate>
                                <!-- <i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</i> -->
                                <i class="el-icon-edit" @click="edit(scope)"></i>
                                &nbsp;
                                <i class="el-icon-delete" @click="delete_(scope)"></i>
                                &nbsp;
                                <!-- <i class="el-icon-lx-friend" @click="change_public(scope)" v-if="scope.row.is_public==1"><i style="font-size: 5px;">&nbsp;设为私有</i></i>
                                <i class="el-icon-lx-profile" @click="change_public(scope)" v-if="scope.row.is_public==0"><i style="font-size: 5px;">&nbsp;设为公开</i></i>
                                <i></i> -->
                                <i v-if="scope.row.is_public==1"><el-tag @click="change_public(scope)" class="el-icon-lx-friend" size="mini" style="font-size: 5px;" type="danger">&nbsp;设为私有</el-tag></i>
                                <i v-if="scope.row.is_public==0"><el-tag @click="change_public(scope)" class="el-icon-lx-profile" size="mini" style="font-size: 5px;">&nbsp;设为公开</el-tag></i>
                                &nbsp;
                                <i><el-tag class="el-icon-lx-attentionfill" size="mini" style="font-size: 1px;" type="info">{{ scope.row.view_count }}</el-tag></i>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-card>
            </el-col>
        </el-row>
    </div>
</template>

<script>
// import Schart from "vue-schart";
import _global from './Global.vue'
export default {
    name: "dashboard",
    data() {
        return {
            userImgPath: "data:image/png;base64," + localStorage.getItem("user_img_path"),
            name: localStorage.getItem("ms_username"),
            agree_count : localStorage.getItem("agree_count"),
            view_count : parseInt(localStorage.getItem("view_count")),
            blog_count : parseInt(localStorage.getItem("blog_count")),
            todoList: [],
            lastLoginTime: localStorage.getItem('last_sign_time'),
            lastLoginArea: localStorage.getItem('last_sign_area'),
            value: 1,
            text: ['崭新', '无奇', '小牛', '大成', '至尊'],
            dynamicTags: JSON.parse(localStorage.getItem("slogan")),
            inputVisible: false,
            inputValue: '',
            activeName: 'hot_article',
        };
    },
    components: {
        // Schart
    },
    computed: {
        role() {
            return this.name === "admin" ? "超级会员" : "普通用户";
        }
    },
    created() {
        this.init_func()
    },

    methods: {
        handleClick(tab, event) {
            console.log(tab, event);
            this.init_func()
        },
        init_func() {
            fetch(`http://${_global.request_host}:${_global.reuqest_port}/article/new_list?types=1&sort=` + this.activeName, {
                headers:{
                    'Content-Type': 'application/x-www-form-urlencoded',
                    'token': localStorage.getItem('token')
                },
                mode: "cors"
            })
            .then(resp => resp.json())
            .then(json => {
                if(json.code==200){
                    this.todoList = json.results.message
                }
            })
        },
        change_public(scope) {
            fetch(`http://${_global.request_host}:${_global.reuqest_port}/article/change_public?aid=` + scope.row.aid, {
                mode: "cors"
            })
            .then(resp => resp.json())
            .then(json => {
                if(json.code==200){
                    console.log('success')
                    this.init_func()
                }
            })
        },
        changeDate() {
            const now = new Date().getTime();
            this.data.forEach((item, index) => {
                const date = new Date(now - (6 - index) * 86400000);
                item.name = `${date.getFullYear()}/${date.getMonth() +
                    1}/${date.getDate()}`;
            });
        },
        watch_detail(scope) {
            localStorage.setItem('watch_id', scope.row.aid)
            this.view_count = 1 + this.view_count
            localStorage.setItem('view_count', this.view_count)
            this.$router.push('/MDDetail')
        },
        edit(scope) {
            console.log("编辑任务")
            console.log(scope)
            localStorage.setItem('editor_aid', scope.row.aid)
            this.$router.push('editorMarkdown')
        },
        delete_(scope) {
            this.$confirm('确定删除这个文章吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                fetch(`http://${_global.request_host}:${_global.reuqest_port}/article/delete_?aid=` + scope.row.aid, {
                    headers:{
                        'Content-Type': 'application/x-www-form-urlencoded',
                            'token': localStorage.getItem('token')
                    }
                })
                .then(resp => resp.json())
                .then(json => {
                    // console.log(json)
                    if(json.code==200){
                        // console.log('success')
                        this.$message({
                            type: 'success',
                            message: scope.row.title + '已删除!'
                        });
                        this.blog_count = this.blog_count - 1
                        localStorage.setItem('blog_count', this.view_count)
                        this.init_func()
                    }else{
                        this.$message({
                            type: 'warning',
                            message: json.results.message
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                });
            });
        },
        handleClose(tag) {
            this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
        },
        showInput() {
            this.inputVisible = true;
            this.$nextTick(_ => {
                console.log(_)
                this.$refs.saveTagInput.$refs.input.focus();
            });
        },
        handleInputConfirm() {
            let inputValue = this.inputValue;
            if (inputValue) {
            this.dynamicTags.push(inputValue);
            }
            this.inputVisible = false;
            this.inputValue = '';
        }
    }
};
</script>

<style scoped>
#dashboard {
    min-width: 600px;
}

.el-row {
    margin-bottom: 20px;
}

.grid-content {
    display: flex;
    align-items: center;
    height: 50px;
}

.grid-cont-right {
    flex: 1;
    text-align: center;
    font-size: 10px;
    color: #999;
}

.grid-num {
    font-size: 20px;
    font-weight: bold;
}

.grid-con-icon {
    font-size: 50px;
    width: 50px;
    height: 50px;
    text-align: center;
    line-height: 50px;
    color: #fff;
}

.grid-con-1 .grid-con-icon {
    background: rgb(45, 140, 240);
}

.grid-con-1 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-2 .grid-con-icon {
    background: rgb(100, 213, 114);
}

.grid-con-2 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-3 .grid-con-icon {
    background: rgb(242, 94, 67);
}

.grid-con-3 .grid-num {
    color: rgb(242, 94, 67);
}

.user-info {
    display: flex;
    align-items: center;
    padding-bottom: 20px;
    margin-bottom: 20px;
}

.user-avator {
    width: 120px;
    height: 120px;
    border-radius: 50%;
}

.user-info-cont {
    flex: 1;
    font-size: 14px;
    color: #999;
}

.user-info-cont div:first-child {
    font-size: 30px;
    color: #222;
}

.user-info-list {
    font-size: 14px;
    color: #999;
    line-height: 25px;
}

.user-info-list span {
    margin-left: 10px;
}

.mgb20 {
    margin-bottom: 20px;
}

.todo-item {
    font-size: 13px;
}

.todo-item-del {
    text-decoration: line-through;
    color: #999;
}

.schart {
    width: 100%;
    height: 300px;
}

.el-tag + .el-tag {
    margin-left: 10px;
    margin-top: 10px;
}
.button-new-tag {
    margin-left: 10px;
    height: 32px;
    margin-top: 10px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
}
.input-new-tag {
    width: 90px;
    margin-top: 10px;
    margin-left: 10px;
    vertical-align: bottom;
}
</style>
